#
# specify target name
#
TARGET := lcd-tools

srctree         := $(shell pwd)
export srctree

#
# specify the output path
#

# If you want the default output to the specified directory
# Please add the following two lines
# KBUILD_OUTPUT	:= $(srctree)/output
# export KBUILD_OUTPUT

ifeq ("$(origin O)", "command line")
  KBUILD_OUTPUT := $(O)
endif

ifneq ($(KBUILD_OUTPUT),)
saved-output    := $(KBUILD_OUTPUT)
KBUILD_OUTPUT   := $(shell mkdir -p $(KBUILD_OUTPUT) && cd $(KBUILD_OUTPUT) && /bin/pwd)
$(if $(KBUILD_OUTPUT),,$(error failed to create output directory "$(saved-output)"))
export KBUILD_OUTPUT
out             := $(KBUILD_OUTPUT)/
else
out             :=
endif

#
# Build target
#
$(out)$(TARGET):

include $(srctree)/scripts/Makefile.include
-include $(srctree)/include/config/auto.conf

#
# specify the compiler
#
HOSTCC          = gcc

CROSS_COMPILE   ?= $(CONFIG_CROSS_COMPILE)
AS              = $(CROSS_COMPILE)as
LD              = $(CROSS_COMPILE)ld
CC              = $(CROSS_COMPILE)gcc
CPP             = $(CC) -E
AR              = $(CROSS_COMPILE)ar
NM              = $(CROSS_COMPILE)nm
STRIP           = $(CROSS_COMPILE)strip
OBJCOPY         = $(CROSS_COMPILE)objcopy
OBJDUMP         = $(CROSS_COMPILE)objdump
export AS LD CC CPP AR NM STRIP OBJCOPY OBJDUMP

CFLAGS          := -Wall -O2 -g \
		   -include include/generated/autoconf.h \
		   -Iinclude \
		   -Icomponents/include \
		   -Idrivers/include \
		   -Imodules/include

LDFLAGS         := -ldl -lm -lpthread
export CFLAGS LDFLAGS EXCFLAGS

#
# Accept the target directory path parameter
# Remove the final / of the path
# Remove the Spaces before and after
# Parsed file name
#
param-dir = $(dir)
_target-path = $(filter %/, $(param-dir))
__target-path = $(if $(_target-path),$(patsubst %/,%,$(_target-path)),$(param-dir))
target-path = $(strip $(__target-path))
target-name = $(notdir $(target-path))

#
# simplified printing information
#
ifeq ("$(origin V)", "command line")
  KBUILD_VERBOSE = $(V)
endif
ifndef KBUILD_VERBOSE
  KBUILD_VERBOSE = 0
endif

ifeq ($(KBUILD_VERBOSE),1)
  quiet =
  Q =
else
  quiet=quiet_
  Q = @
endif

obj-y				+= modules/
obj-y				+= platforms/
obj-y				+= drivers/

$(out)$(TARGET): $(out)built-in.o
	$(call cmd,mld)
	$(call cmd,strip)

$(out)built-in.o: autoconf
	$(Q)$(MAKE) $(mbuild)=.

%_defconfig: scripts/kconfig/conf FORCE
	scripts/kconfig/conf --defconfig configs/$@ Kconfig

menuconfig: scripts/kconfig/mconf scripts/kconfig/conf FORCE
	$(Q)scripts/kconfig/mconf Kconfig
	$(Q)mkdir -p include/config
	$(Q)mkdir -p include/generated
	$(Q)scripts/kconfig/conf --syncconfig Kconfig

autoconf: scripts/kconfig/conf FORCE
	$(Q)mkdir -p include/config
	$(Q)mkdir -p include/generated
	$(Q)$< --syncconfig Kconfig

scripts/kconfig/conf:
	$(Q)$(HOSTCC) -o $@ scripts/kconfig/conf.c \
			scripts/kconfig/confdata.c \
			scripts/kconfig/expr.c \
			scripts/kconfig/lexer.lex.c \
			scripts/kconfig/menu.c \
			scripts/kconfig/parser.tab.c \
			scripts/kconfig/preprocess.c \
			scripts/kconfig/symbol.c \
			scripts/kconfig/util.c

scripts/kconfig/mconf:
	$(Q)$(HOSTCC) -o $@ scripts/kconfig/mconf.c \
			scripts/kconfig/lxdialog/checklist.c \
			scripts/kconfig/lxdialog/inputbox.c \
			scripts/kconfig/lxdialog/menubox.c \
			scripts/kconfig/lxdialog/textbox.c \
			scripts/kconfig/lxdialog/util.c \
			scripts/kconfig/lxdialog/yesno.c \
			scripts/kconfig/confdata.c \
			scripts/kconfig/expr.c \
			scripts/kconfig/lexer.lex.c \
			scripts/kconfig/menu.c \
			scripts/kconfig/parser.tab.c \
			scripts/kconfig/preprocess.c \
			scripts/kconfig/symbol.c \
			scripts/kconfig/util.c \
			-Wl,-Bsymbolic-functions -lncursesw -ltinfo

#
# Specify the output path
#
ifneq ($(KBUILD_OUTPUT),)
clean:
	$(call cmd,clr,$(out))
else
clean:
	$(Q)$(MAKE) $(mclean)=.
	$(call cmd,clr,$(TARGET))
endif

distclean: clean
	$(call cmd,clr,.config)
	$(call cmd,clr,.config.old)
	$(call cmd,clr,include/config)
	$(call cmd,clr,include/generated)
	$(call cmd,clr,scripts/kconfig/conf)
	$(call cmd,clr,scripts/kconfig/mconf)

.PHONY:all clean distclean

#
# Separate compilation
# 
lib: $(out)$(target-path)/built-in.o
	$(call cmd,lcc)
	$(call cmd,lar)

ifneq ($(KBUILD_OUTPUT),)
lib_clean:
	$(call cmd,clr,$(out)$(target-path)/)
else
lib_clean:
	$(Q)$(MAKE) $(mclean)=$(target-path)
	$(call cmd,clr,$(out)$(target-path)/$(target-name).a)
	$(call cmd,clr,$(out)$(target-path)/$(target-name).so)
endif

$(out)$(target-path)/built-in.o:
	$(Q)$(MAKE) $(mbuild)=$(target-path)

elf:
	$(Q)make -C $(target-path) --no-print-directory

elf_clean:
	$(Q)make -C $(target-path) --no-print-directory clean

#
# simplified printing information
# mld: main ld
# lcc: lib cc
# lar: lib ar
# clr: clean
#
quiet_cmd_mld      = LD      $@
      cmd_mld      = $(CC) -o $@ $^ $(LDFLAGS)

quiet_cmd_lcc      = CC      $(out)$(target-path)/$(target-name).so
      cmd_lcc      = $(CC) -o $(out)$(target-path)/$(target-name).so -shared -fPIC $^

quiet_cmd_lar      = AR      $(out)$(target-path)/$(target-name).a
      cmd_lar      = $(AR) cr $(out)$(target-path)/$(target-name).a $^

quiet_cmd_clr      = CLEAR   $(2)
      cmd_clr      = rm -rf $(2)

quiet_cmd_cp       = CP      $(2)
      cmd_cp       = cp -a $(2) $(3)

quiet_cmd_strip    = STRIP   $@
      cmd_strip    = $(STRIP) $@

cmd = @$(if $($(quiet)cmd_$(1)),echo '  $($(quiet)cmd_$(1))' &&) $(cmd_$(1))
